﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Neat;
using Neat.MenuSystem;
using Neat.EasyMenus;
using Neat.GUI;
using Neat.Mathematics;
using Neat.Graphics;
using RattlerRace;

namespace NeatStarter
{
    public class GameOverScreen : Screen
    {
        private bool typeEnabled = true;

        public GameOverScreen(NeatGame Game)
            : base(Game)
        {
        }
        Label userField;
        public override void Initialize()
        {
            base.Initialize();
            Label hello = Form.NewControl("hello", new Label()) as Label;
            hello.Caption = "GAME OVER!";
            hello.Position = new Vector2(100, 100);
            hello.ForeColor = Color.White;

            Label userLabel = Form.NewControl("userLabel", new Label()) as Label;
            userLabel.Caption = "Player name:";
            userLabel.Position = new Vector2(100, 150);
            userLabel.ForeColor = Color.White;

            userField = Form.NewControl("userField", new Label()) as Label;
            userField.Caption = "";
            userField.Position = new Vector2(300, 150);
            userField.ForeColor = Color.Red;
            userField.OnPress = userField_OnPress;

            Button mybutton = Form.NewControl("mybutton", new Button()) as Button;
            mybutton.Caption = "Play again";
            mybutton.Position = new Vector2(100, 250);
            mybutton.ForeColor = Color.White;
            mybutton.HoverTintColor = Color.Green;
            mybutton.Size = new Vector2(400, 70);
            mybutton.OnPress = mybutton_OnPress;

            Button optionsbutton = Form.NewControl("optionsbutton", new Button()) as Button;
            optionsbutton.Caption = "Main menu";
            optionsbutton.Position = new Vector2(100, 350);
            optionsbutton.ForeColor = Color.White;
            optionsbutton.HoverTintColor = Color.Green;
            optionsbutton.Size = new Vector2(400, 70);
            optionsbutton.OnPress = optionsbutton_OnPress;

            Button resultsbutton = Form.NewControl("resultsbutton", new Button()) as Button;
            resultsbutton.Caption = "Results";
            resultsbutton.Position = new Vector2(100, 450);
            resultsbutton.ForeColor = Color.White;
            resultsbutton.HoverTintColor = Color.Green;
            resultsbutton.Size = new Vector2(400, 70);
            resultsbutton.OnPress = resultsbutton_OnPress;


            Form.HasMouse = true;
            game.ShowMouse = true;
        }

        void userField_OnPress()
        {
            typeEnabled = true;
        }


        void resultsbutton_OnPress() {
            Game1.GameStats.PlayerName = userField.Caption;
            Game1.Results.Add(Game1.GameStats);
            Game1.GameStats = new GameStats(3, 0, Game1.GameOptions.ZacetniLevel);
            base.game.Console.Run("e_show resultscreen");
        }

        void mybutton_OnPress()
        {
            Game1.GameStats.PlayerName = userField.Caption;
            Game1.Results.Add(Game1.GameStats);
            Game1.GameStats = new GameStats(3, 0, Game1.GameOptions.ZacetniLevel);
            base.game.Console.Run("e_show gamescreen");
        }

        void optionsbutton_OnPress()
        {
            Game1.GameStats.PlayerName = userField.Caption;
            Game1.Results.Add(Game1.GameStats);
            Game1.GameStats = new GameStats(3, 0, Game1.GameOptions.ZacetniLevel);
            base.game.Console.Run("e_show mainmenu");
        }

        KeyboardState preyKeyboardState, currentKeyboardState;
        MouseState prevMouseState, currentMouseState;
        public override void HandleInput(GameTime gameTime)
        {
            currentMouseState = Mouse.GetState();
            if (currentMouseState.LeftButton == ButtonState.Pressed)
                typeEnabled ^= true;

            if (!typeEnabled) return;

            base.HandleInput(gameTime);
            preyKeyboardState = currentKeyboardState;
            currentKeyboardState = Keyboard.GetState();

            if (currentKeyboardState != preyKeyboardState)
            {
                if (currentKeyboardState.GetPressedKeys().Length > 0)
                {
                    Keys tipka = currentKeyboardState.GetPressedKeys()[0];
                    if (currentKeyboardState.IsKeyDown(Keys.Back))
                        userField.Caption = userField.Caption.Remove(userField.Caption.Length - 1);
                    else
                        userField.Caption += tipka.ToString();
                }
            }
        }
    }
}
